# Contributor: Jean-Louis Fuchs <jean-louis.fuchs@adfinis-sygroup.ch>
# Maintainer: Jean-Louis Fuchs <jean-louis.fuchs@adfinis-sygroup.ch>
pkgname=unison
# Note: unison breaks compatibility between minor (major.minor) versions.
pkgver=2.48.15_p4
_pkgver=${pkgver/_p/v}
_majorver=${_pkgver%.*}  # x.y
pkgrel=1
pkgdesc="Efficient file-synchronization tool"
url="https://www.cis.upenn.edu/~bcpierce/unison/"
# ocaml is not built for x86, armhf, s390x, mips, mips64
arch="all !x86 !armhf !armv7 !s390x !mips !mips64"
license="GPL-3.0-or-later"
makedepends="ocaml ocaml-lablgtk-dev linux-headers emacs-nox bash"
subpackages="$pkgname-gui"
source="unison-$_pkgver.tar.gz::https://github.com/bcpierce00/unison/archive/v$_pkgver.tar.gz
	fix-inotify-check.patch
	makefile-fix-clean.patch
	fix-for-lablgtk-2.18.6.patch
	ocaml-4.08.patch"
# TODO: Enable check once this issue is fixed:
# https://github.com/bcpierce00/unison/issues/73
options="!check"
builddir="$srcdir/unison-$_pkgver/src"

build() {
	# ocamlopt is a front-end for gcc which will create optimized ocaml
	# binaries. It will call gcc, but it hasn't implemented all gcc options.
	# -ccopt is the way to pass arbitrary options.
	local _cflags=''
	local i; for i in $CFLAGS; do
		_cflags="$_cflags -ccopt $i"
	done
	export CFLAGS="$_cflags"

	local ui; for ui in text gtk2; do
		make -j1 clean
		make -j1 mkProjectInfo
		make -j1 UISTYLE="$ui" DEBUGGING=false THREADS=true
		mv unison unison-$ui
	done
}

package() {
	install -m 755 -D unison-text \
		"$pkgdir"/usr/bin/unison-text-$_majorver
	install -m 755 -D unison-fsmonitor \
		"$pkgdir"/usr/bin/unison-fsmonitor-$_majorver

	cd "$pkgdir"/usr/bin
	ln -s unison-text-$_majorver unison-text
	ln -s unison-text unison
}

gui() {
	pkgdesc="$pkgdesc (GTK+2 GUI)"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel gtk+2.0"

	cd "$builddir"
	install -m 755 -D unison-gtk2 \
		"$subpkgdir"/usr/bin/unison-gtk2-$_majorver
	ln -s unison-gtk2-$_majorver "$subpkgdir"/usr/bin/unison-gtk2
}

sha512sums="658b37fd18d8dee517e88ad707f7e80c1731542a0e07e6a8d56e6598e38bcf405cdb8db224274bf9b3319ed87efc45057d1f79e6437c4ca53afe9cec4afe6c78  unison-2.48.15v4.tar.gz
53ef0b9bd5632509cad69d60924da36836017574a59cd373aa44710c2d82b1329ab406753f430c98be8527428bd9a4847dde372dc5074781eb4a83de4e300d77  fix-inotify-check.patch
cd8b9b6edc22c586f8f3fa566224c465a007b46b86766c25c2348f1a8eadb8faf28d6a8e4747611f8550f938ed52cb62de00cd9ca175e27b57cd81d6c286229a  makefile-fix-clean.patch
b7ffd449ff54f03f4f7ca11dfeb1cd4ebb757f60bd924797c4fb48e63bbc99188e353b53e240043e99d4915bf1c2ed54444bfe7c4b0393c5ad010b8ac16249e2  fix-for-lablgtk-2.18.6.patch
302d070003e396982c35260ee8dd1569b272a5c17f21eabd92ae41bd6fe8570a117e769f5e26a26bf9f4a7a418c016a73c1edf8a11f552c7b8eaf9dadd4113ac  ocaml-4.08.patch"
